import { Product, ProductFilterData } from "../models/product";

export const GET_PRODUCT = 'GET_PRODUCT';
export const GET_PRODUCT_SUCCESS = 'GET_PRODUCT_SUCCESS';

export interface GetProductPayload{
    sortBy: string,
    order: string,
    limit: number
}
// sortBy=createdAt&order=asc&limit=10
export interface GetProductAction {
    type: typeof GET_PRODUCT,
    sortBy: string,
    order: string,
    limit: number
}

export const getProduct = (
    sortBy: string,
    order: string = 'desc',
    limit: number = 10
    ): GetProductAction => ({
    type: GET_PRODUCT,
    sortBy,
    order,
    limit
})

export interface GetProductSuccessAction {
    type: typeof GET_PRODUCT_SUCCESS,
    payload:Product[],
    sortBy: string
}

export const getProductSuccess = (sortBy: string, payload: Product[]) => ({
    type: GET_PRODUCT_SUCCESS,
    payload,
    sortBy
})
/**
 * 搜索
 */
export const GET_PRODUCT_BY_SEARCHVALUE = 'GET_PRODUCT_BY_SEARCHVALUE'
export const GET_PRODUCT_BY_SEARCHVALUE_SUCCESS = 'GET_PRODUCT_BY_SEARCHVALUE_SUCCESS'

export interface SearchPayload {
    category: string,
    search: string
}
export interface GetProductsBySearchValueAction {
    type: typeof GET_PRODUCT_BY_SEARCHVALUE,
    searchValue:SearchPayload
}

export const GetProductBySearchValue = (searchValue: SearchPayload): GetProductsBySearchValueAction => ({
    type: GET_PRODUCT_BY_SEARCHVALUE,
    searchValue
})

export interface GetProductsBySearchValueSuccessAction {
    type: typeof GET_PRODUCT_BY_SEARCHVALUE_SUCCESS,
    payload: Product[]
}

export const GetProductBySearchValueSuccess = (payload: Product[]) : GetProductsBySearchValueSuccessAction=> ({
    type: GET_PRODUCT_BY_SEARCHVALUE_SUCCESS,
    payload
})

/**
 * 商品过滤
 */

export const GETFILTERPRODUCT = 'GETFILTERPRODUCT'
export const GETFILTERPRODUCT_SUCCESS = 'GETFILTERPRODUCT_SUCCESS'

export interface GetFilerProductPayload {
    order?: string,
    limit?: number,
    sortBy?: string,
    skip: number,
    filters?: {
        category: string[],
        price : number[]
    }
}

export interface GetFillterProductAction {
    type: typeof GETFILTERPRODUCT,
    payload: GetFilerProductPayload
}

export const getFilterProduct = (payload: GetFilerProductPayload) : GetFillterProductAction=> ({
    type: GETFILTERPRODUCT,
    payload
})

export interface GetFillterProductSuccessAction {
    type: typeof GETFILTERPRODUCT_SUCCESS,
    payload: ProductFilterData,
    skip: number
}

export const getFilterProductSuccess = ( payload :ProductFilterData, skip : number) : GetFillterProductSuccessAction => ({
    type: GETFILTERPRODUCT_SUCCESS,
    payload,
    skip
})

/**
 * 通过产品id获取产品数据
 */
export const GETPRODUCTBYID = 'GETPRODUCTBYID'
export const GETPRODUCTBYID_SUCCESS = 'GETPRODUCTBYID_SUCCESS'

export interface GetProductByIDAction {
    type: typeof GETPRODUCTBYID,
    payload: string
}

export const getProductByID = (payload: string): GetProductByIDAction => ({
    type: GETPRODUCTBYID,
    payload
})

export interface GetProductByIDSuccessAction {
    type: typeof GETPRODUCTBYID_SUCCESS,
    result: Product
}

export const getProductByIDSuccess = (result: Product): GetProductByIDSuccessAction => ({
    type: GETPRODUCTBYID_SUCCESS,
    result
})

export type ProductUnionType = GetProductAction 
| GetProductSuccessAction 
| GetProductsBySearchValueAction 
| GetProductsBySearchValueSuccessAction
| GetFillterProductAction
| GetFillterProductSuccessAction
| GetProductByIDAction | GetProductByIDSuccessAction